$(function(){
    //点击搜索框显示搜索历史记录
    $("#search-input").on("click",function(){
        $("#classify").css("display","none");
        $("#centerHistory").css("display","block");
    });
    var currentCity=SYS_GLOBAL_CONFIG.getCurrentCity("currentCity");
    
    console.log("测试一下"+currentCity);
    var l_s;
    if(window.localStorage){
        l_s=window.localStorage;      
    }else{
        l_s={};
    }
    //条状到不同的搜索页面
    $(".c-ul li").on("click",function(){
        var link=$(this).find("a").attr("data-link");
        if($("#centerHistory").css("display")=="block"){
            l_s.search_state=1;
        }else{
            l_s.search_state=0;
        }
        location.href="./"+link+".html?currentCity="+currentCity;
    });
    var url=SYS_GLOBAL_CONFIG.reqUrl,
    asyn="true",
    type="post";
    var address=currentCity;//当前城市名
    var areas=""||address;//当前街道
    var productTypeId=0;//默认当前类型id
    var pdTypeId=0;//默认当前类型的父id
    var areaId="";//当前地区的id
    var phoneNO="";
    var currentPage=1;//默认从第一页开始显示
    var totalPage=1;//默认总页数
    var show_count=5;//默认一页显示的条数
    var shopName="";//默认搜索商家名
    var requestData2;
    var requestData3;
    var dataShop=JSON.stringify({
        "code":"getShopList",
        "verson":"1.0",
        "data":{
            "phoneNO":phoneNO,
            "areaId":areaId,
            "salesManId":"",
            "address":address,
            "sellerType":"",
            "currentPage":currentPage,
            "showCount":show_count,
            "shopName":shopName,
            "productTypeId":productTypeId,
            "parentPdTypeId":pdTypeId
        }
    });
    function getData(url,requestData,call_func,asyn,type){
        console.log("请求参数"+requestData);
        $.ajax({
            url:url,
            type:type,
            datatype:"json",
            contentType:"application/json",
            data:requestData,
            async:asyn,
            success:function(data){
                call_func(data);  
            },
            error:function(msg){
                console.log("异常："+msg);
            }   
        });        
    }
    //阻止滚动
    var unScroll=function(){
        $('html,body').css({"height":"100%","overflow":"hidden"}); 
    }
    //允许滚动
    var doScroll=function(){
        $('html,body').css({"height":"auto","overflow":"auto"});
    }
    //设置页面滚动
    function setScroll(ele){
        console.log(ele);
        console.log("aa11"+$(ele).css("display"));
        if($(ele).css("display")=="block"){
            unScroll();//阻止滚动
        }else{
            doScroll();//允许滚动
        }
    }
    //获取附近店铺信息
    getData(url,dataShop,call_func1,asyn,type);
    function call_func1(data){
            var dataS=data.data.datas;
            var s_len=dataS.length;
            var img="";
            var registr="";
            var attr="";
            totalPage=data.data.totalPage;
            var li="";
            var downS='';
            for(var i=0;i<s_len;i++){
                if(dataS[i].seller){
                    if(dataS[i].seller.isSellerAuth=="1"){
                        img='<img class="img3" src="./images/vip_icon.png" alt="">';
                        registr="(已认证)";
                    }else{
                        img="";
                        registr="(未认证)";
                    }    
                }
                var tagStr='';
                var tagS='';
                if(dataS[i].sellerFees){
                    if(dataS[i].sellerFees.length>0){
                        console.log(dataS[i].sellerFees.length);
                        if(dataS[i].sellerFees.length>0){
                            var SellerFree=dataS[i].sellerFees[0].isAvaliable;
                            if(SellerFree=="1"){//已开通增值服务
                                var s_tag=dataS[i].tagServices;
                                var ss_len=s_tag.length;
                                if(ss_len>2){
                                    downS='<i class="down"></i>';
                                }
                                for(var s=0;s<ss_len;s++){
                                    tagStr+='<p class="tags_info"><img src="'+s_tag[s].tagIconPath+'" alt=""><span>'+s_tag[s].userRemark+'</span></p>';
                                    if(s<3){
                                        tagS+='<span>'+s_tag[s].name+'</span>';
                                    }
                                }        
                            }       
                        }
                    }    
                }

               li+='<div class="detail-shop" data-shopId="'+dataS[i].shopId+'">'+
                    ' <img class="img2" src="'+dataS[i].logoImgPath+'" alt="">'+
                    '<div class="introduce">'+
                    ' <div class="introduce-detail">'+
                    '<div class="introduce-name">'+
                    '<span class="span-detail"><span class="span-name">'+dataS[i].shopName+'</span>'+ 
                    ''+img+'</span><p class="span-tags">'+tagS+'</p>'+
                    ' <span class="cess r1">'+dataS[i].shopTypeName+'</span><span class="regstr r1">'+registr+'</span> </div>'+
                    '<div class="clears"></div></div></div><div class="clears"></div><div class="zwl-benefit max2_h">'+tagStr+downS+'</div></div>';
            }
            $("#detail").append(li);
            var down_flag=1;
            if(downS!=''){
                $(".zwl-benefit i").off("click");
                $(".zwl-benefit i").on("click",function(){
                    if(down_flag==1){  
                        $(this).addClass("up").removeClass("down").parent(".zwl-benefit").addClass("auto_h").removeClass("max2_h");
                        down_flag=0;
                    }else{
                        $(this).addClass("down").removeClass("up").parent(".zwl-benefit").addClass("max2_h").removeClass("auto_h");
                        down_flag=1;
                    }
                });    
            }
        $(".detail-shop .img2,.detail-shop .introduce").on("click",function(){
                var shopId =$(this).parent(".detail-shop").attr("data-shopId");
                url = "shopDetail.html?shopId="+shopId;//此处拼接内容
                location.href = url;
            });
    } 
    
    var defaultColor="#333";
    var changeColor="#36c7b7";
    var htmlSize=parseInt($("html").css("fontSize"));
    var h=parseInt($(".searchShopsBody").css("height"));
    var bottomh=2.5*htmlSize;
    /**
     *阴影控制
     **/
    function shadow(){
        var top=$(this).scrollTop();
        if(top>5*htmlSize){
            $(".shadow-top3725").addClass("topAfter").removeClass("topBefore");
        }else{
            $(".shadow-top3725").removeClass("topAfter").addClass("topBefore");
        }
    }
    $(document).on("scroll",$(this),shadow);
    $(".shadow-top3725").on("click",function(){
        $(this).hide();
        doScroll();
        for(var i=0,len=$(".shopsSelect").length;i<len;i++){
            if($(".shopsSelect")[i].style.display!="none"){
                $(".shopsSelect")[i].style.display="none";
            }
        }
        $(".shop-nav-li").find("i").attr("class","arrow-bottom");
        $(".shop-nav-li").css("color",defaultColor);
    });


    //点击显示地区s
    $("#area").on("click",function(){
        var classname=$(this).find("i").attr("class");
        console.log(classname);
        var dataCity=JSON.stringify({
            "code":"getDictAreaList",
            "version":"1.0",
            "data":{
                "level":"2",
                "fullName":address
            }
        });
        console.log(classname.indexOf("arrow-bottom")>-1);
        if(classname.indexOf("arrow-bottom")>-1){
            getData(url,dataCity,call_func2,asyn,type);    
        }
        $("#shopArea").slideToggle(100,function(){
            setScroll("#shopArea");
        }).siblings(".shopsSelect").hide();
        handle($(this));
    });
    
    
    /**
     *获取全部城市
     **/
    function call_func2(data){
        console.log(data);
        var datas=data.data.dictAreas;
        var areaLen=datas.length;
        var cityStr='<li data-areaId="" data-city=""  class="area-li bgf036c">全城</li>';
        for(var i=0;i<areaLen;i++){
            cityStr+='<li data-areaId="'+datas[i].areaId+'" data-city="'+datas[i].fullName+'" class="area-li  ">'+datas[i].areaName+'</li>';    
        }
        $(".area-nav").html(cityStr);
        
        $(".area-nav .area-li").on("click",function(){
            $(this).addClass("bgf036c").siblings(".area-li").removeClass("bgf036c");
            areaId=$(this).attr("data-areaId");
            var cityname=$(this).attr("data-city");
            var curr_city=cityname;
            var dataArea=JSON.stringify({
                "code":"getDictAreaList",
                "version":"1.0",
                "data":{
                    "level":"3",
                    "fullName":curr_city
                }
            });
           
            if(cityname==""){
                $("#area-nav-item").html("");
                $("#area-nav-item").html('<li><span>全城</span></li>');
                $("#area-nav-item li").on("click",function(){
                    $("#detail").html("");
                    current_Page=1;
                    areas=$(this).attr("data-area");
                    var area_data=JSON.stringify({
                        "code":"getShopList",
                        "verson":"1.0",
                        "data":{
                            "phoneNO":phoneNO,
                            "areaId":areaId,
                            "salesManId":"",
                            "address":address,
                            "sellerType":"",
                            "currentPage":currentPage,
                            "showCount":show_count,
                            "shopName":shopName,
                            "productTypeId":productTypeId,
                            "parentPdTypeId":pdTypeId
                        }
                    });
                    var city=$(this).find("span").html();//当前街道
                    $("#currentCity").html($(this).html());
                    getData(url,area_data,call_func1,asyn,type);
                    $("#shopSort").hide();
                    $(".shadow-top3725").hide();
                    $("#area").css("color",defaultColor).find("i").addClass("arrow-bottom").removeClass("arrow-bottom36c").removeClass("arrow-top36c").removeClass("arrow-top");
                    $("#shopArea").slideToggle(100,function(){
                        setScroll("#shopArea");
                    }).siblings(".shopsSelect").hide();    $("#sort").css("color",defaultColor).find("i").attr("class","arrow-bottom");
  
                });
            }else{
                getData(url,dataArea,call_func_area,asyn,type);
            }
        });
    }
    
     /**
     *获取全部产品类型
     **/
    function call_func3(data){
        console.log(data);
        var dataType=data.data.productTypes;
        var d_len=dataType.length;
        var  typeStr='<li data-typeId="0" class="sort-li bgf036c">全部分类</li>';
        for(var i=0;i<d_len;i++){
            typeStr+='<li data-typeId="'+dataType[i].pdTypeId+'" class="sort-li topborf0f">'+dataType[i].pdTypeName+'</li>';
        }
        $(".sort-nav").html(typeStr);
        //点击不同的类型加载不同的数据
        $(".sort-nav .sort-li").on("click",function(){
            $(this).addClass("bgf036c").siblings(".sort-li").removeClass("bgf036c");
            console.log("当前商品项"+$(this).attr("data-typeId"));
            pdTypeId=parseInt($(this).attr("data-typeId"));

            var sort_data=JSON.stringify({
                "code":"getPdOrServTypeList",
                "version":"1.0",
                "data":{
                    "parentServiceId":0,
                    "type":"PRODUCT",
                    "parentPdTypeId":pdTypeId
                }
            });
            if(pdTypeId==0){
                $("#sort-nav-item").html('<li data-typeId="0" class="sort-li">全部分类</li>');
                $("#sort-nav-item li").on("click",function(){
                    $("#detail").html("");
                    current_Page=1;     
                    productTypeId=$(this).attr("data-typeId");
                    var sort_data=JSON.stringify({
                        "code":"getShopList",
                        "verson":"1.0",
                        "data":{
                            "phoneNO":phoneNO,
                            "areaId":areaId,
                            "salesManId":"",
                            "address":address,
                            "sellerType":"",
                            "currentPage":currentPage,
                            "showCount":show_count,
                            "shopName":shopName,
                            "productTypeId":productTypeId,
                            "parentPdTypeId":pdTypeId
                        }
                    });
                    $("#currentSort").html($(this).html());
                    getData(url,sort_data,call_func1,asyn,type);
                    $("#shopArea").hide();
            $(".shadow-top3725").hide();
            $("#sort").css("color",defaultColor).find("i").addClass("arrow-bottom").removeClass("arrow-bottom36c").removeClass("arrow-top36c").removeClass("arrow-top");
            $("#shopSort").slideToggle(100,function(){
                setScroll("#shopSort");
            }).siblings(".shopsSelect").hide();    $("#area").css("color",defaultColor).find("i").attr("class","arrow-bottom");
                });
            }else{
                getData(url,sort_data,call_func_sort,asyn,type);
            }
        });
    }
    function call_func4(data){
        console.log(data);
    }
    //子类详细信息
    function call_func_sort(data){
        console.log(data);
        var datali="";
        var datas=data.data.productTypes;
        var areaLen=datas.length;
        for(var i=0;i<areaLen;i++){
            datali+='<li data-typeId="'+datas[i].pdTypeId+'"><span>'+datas[i].pdTypeName+'</span></li>';
        }
        $("#sort-nav-item").html(datali);
        $("#sort-nav-item li").on("click",function(){
            $("#detail").html("");
            current_Page=1;
            productTypeId=parseInt($(this).attr("data-typeId"));
            var sort_data=JSON.stringify({
                "code":"getShopList",
                "verson":"1.0",
                "data":{
                    "phoneNO":phoneNO,
                    "areaId":areaId,
                    "salesManId":"",
                    "address":address,
                    "sellerType":"",
                    "currentPage":currentPage,
                    "showCount":show_count,
                    "shopName":shopName,
                    "productTypeId":productTypeId,
                    "parentPdTypeId":pdTypeId
                }
            });
            $("#currentSort").html($(this).html());
            getData(url,sort_data,call_func1,asyn,type);
            $("#shopArea").hide();
            $(".shadow-top3725").hide();
            $("#sort").css("color",defaultColor).find("i").addClass("arrow-bottom").removeClass("arrow-bottom36c").removeClass("arrow-top36c").removeClass("arrow-top");
            $("#shopSort").slideToggle(100,function(){
                setScroll("#shopSort");
            }).siblings(".shopsSelect").hide();    $("#area").css("color",defaultColor).find("i").attr("class","arrow-bottom");
        });
    }
    //获取城市地区
    function call_func_area(data){
        console.log(data);
        var datali="";
        var datas=data.data.dictAreas;
        var areaLen=datas.length;
        for(var i=0;i<areaLen;i++){
            datali+='<li data-areaId="'+datas[i].areaId+'" data-area="'+datas[i].fullName+'"><span>'+datas[i].areaName+'</span></li>';
        }
        $("#area-nav-item").html(datali);
        $("#area-nav-item li").on("click",function(){
            $("#detail").html("");
            current_Page=1;
            areaId=$(this).attr("data-areaId");
            var area_data=JSON.stringify({
                "code":"getShopList",
                "verson":"1.0",
                "data":{
                    "phoneNO":phoneNO,
                    "areaId":areaId,
                    "salesManId":"",
                    "address":address,
                    "sellerType":"",
                    "currentPage":currentPage,
                    "showCount":show_count,
                    "shopName":shopName,
                    "productTypeId":productTypeId,
                    "parentPdTypeId":pdTypeId
                }
            });
            var city=$(this).find("span").html();//当前街道
            $("#currentCity").html($(this).html());
            getData(url,area_data,call_func1,asyn,type);
            $("#shopSort").hide();
            $(".shadow-top3725").hide();
            $("#area").css("color",defaultColor).find("i").addClass("arrow-bottom").removeClass("arrow-bottom36c").removeClass("arrow-top36c").removeClass("arrow-top");
            $("#shopArea").slideToggle(100,function(){
                setScroll("#shopArea");
            }).siblings(".shopsSelect").hide();    $("#sort").css("color",defaultColor).find("i").attr("class","arrow-bottom");
        });
    }
    /**
     *点击显示详细分类或者全城信息  
     **/   
    function handle(a){
        var $_this=a;
        $_this.css("color",changeColor);
        if($_this.find("i").attr("class")==="arrow-bottom36c"||$_this.find("i").attr("class")==="arrow-bottom"){
            $_this.find("i").attr("class","arrow-top36c");
            $(".shadow-top3725").show();
        }else{
//                $_this.find("i").attr("class","arrow-bottom36c");
            $(".shadow-top3725").hide();
            $_this.css("color",defaultColor).find("i").attr("class","arrow-bottom");
        }
        $_this.siblings("li").css("color",defaultColor).find("i")
            .addClass("arrow-bottom").removeClass("arrow-bottom36c").removeClass("arrow-top36c").removeClass("arrow-top");
    }
    //点击分类，获取全部类型
    $("#sort").on("click",function(){
        console.log("分类");
        $("#shopSort").slideToggle(100,function(){
            setScroll("#shopSort");
        }).siblings(".shopsSelect").hide();
        var classname=$(this).find("i").attr("class");
        var allProduct=JSON.stringify({
            "code":"getPdOrServTypeList",
            "version":"1.0",
            "data":{
                "parentServiceId":0,
                "type":"PRODUCT",
                "parentPdTypeId":0
            }
        });
        //获取全部产品
        
        if(classname.indexOf("arrow-bottom")>-1){
            getData(url,allProduct,call_func3,asyn,type);
        }
        handle($(this));
    });
    //点击，根据商家名字进行搜索
    $(".search-right .collect").on("click",function(){
        var val=$("#search-input").val();//搜索内容
        search_func(val);
        
    });
    /**
     *搜索商家
     **/                               
    function search_func(val){
        doScroll();
        $("body").css("overflowX","hidden");
        shopName=val;
        var sear_data=JSON.stringify({
            "code":"getShopList",
            "verson":"1.0",
            "data":{
                "phoneNO":phoneNO,
                "areaId":areaId,
                "salesManId":"",
                "address":address,
                "sellerType":"",
                "currentPage":currentPage,
                "showCount":show_count,
                "shopName":shopName,
                "productTypeId":productTypeId,
                "parentPdTypeId":pdTypeId
            }
        });
        console.log(shopName);
        var his_data=shopName
        localStorage.his_data+=his_data+"/";
        $("#search-input").val("");
        $("#detail").html("");
        //搜索
        getData(url,sear_data,call_func1,asyn,type);
        var t=setTimeout(function(){
            l_s.search_state=0;
            console.log("当前的search_state:"+localStorage.search_state);
            $("#centerHistory").css("display","none");//将搜索隐藏
        },100);             
    }              
    
    console.log(localStorage.his_data);
    if(localStorage.his_data==undefined){
        console.log("aaaaaa");
        localStorage.his_data="";       
    }else{
        console.log("aaaaaaaaaaaaaaaaaa");
    }   
    $("#search-input").on("click",function(){
        if(localStorage.his_data.indexOf("/")>-1){
            var h_data_arr=localStorage.his_data.split("/");
            console.log("--"+h_data_arr.length);
            var h_data_str="";
            var len=h_data_arr.length;
            var f=0;//计数器，判断是否超过5
            for(var i=0,len_index=len-1;len_index>=i;len_index--){
                if(f<5){
                    if(h_data_arr[len_index].trim()!=""){
                        h_data_str+='<li><img class="center-img" src="./images/search_icon.png" alt=""><span class="li-span">'+h_data_arr[len_index]+'</span><i></i></li>';
                        f++;
                    }else{
                        h_data_str+='';
                    }
                }else{
                    break;
                }
            }
            console.log(h_data_str);
            $(".history").html(h_data_str);
        }else{
            localStorage.his_data="";
        }
        console.log(localStorage.his_data+"bbbb");
        
        
        $(".history li").on("click",function(){
            var val=$(this).find("span").html();
            search_func(val);
        })
    
    });
    var current_Page=1;
    /**
     *滚动加载更多*
     **/
    $(window).scroll(function(){
        totalheight = parseFloat($(window).height()) + parseFloat($(window).scrollTop())-400;
        $(".data-loading").removeClass("hide").addClass("show");
        if((parseFloat($(document).height())-400)==totalheight){ 
            console.log(totalPage+"---"+current_Page);
            if(totalPage>current_Page){
                current_Page+=1;
                var data_p=JSON.stringify({
                    "code":"getShopList",
                    "verson":"1.0",
                    "data":{
                        "phoneNO":phoneNO,
                        "areaId":areaId,
                        "salesManId":"",
                        "address":address,
                        "sellerType":"",
                        "currentPage":current_Page,
                        "showCount":show_count,
                        "shopName":shopName,
                        "productTypeId":productTypeId,
                        "parentPdTypeId":pdTypeId
                    }
                });
                getData(url,data_p,call_func1,asyn,type);    
            }else{
                $(".data-loading").html("已加载全部");
            }
        } 
     });
    //清除历史记录
    $(".clear-p").on("click",function(){
        localStorage.his_data="";    
        $(".history").html(localStorage.his_data);
        console.log($(".history").html());
    });
});
